const express = require('express')
const app = express()
const http = require('http').createServer(app)
const io = require('socket.io')(http)

app.use(express.json())

// 用户连接处理
io.on('connection', (socket) => {
  console.log('a user connected')

  // 处理消息
  socket.on('message', (msg) => {
    io.emit('message', msg)
  })

  // 用户断开连接
  socket.on('disconnect', () => {
    console.log('user disconnected')
  })
})

// 启动服务器
http.listen(8080, () => {
  console.log('listening on *:8080')
})